package com.example.hyw.szysteam.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.hyw.szysteam.POJO.HomeWork;
import com.example.hyw.szysteam.R;
import com.example.hyw.szysteam.adapter.HomeWorkAdapter;
import com.example.hyw.szysteam.presenter.GetHomeWorkPresenter;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUICenterGravityRefreshOffsetCalculator;
import com.qmuiteam.qmui.widget.pullRefreshLayout.QMUIPullRefreshLayout;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/6/8.
 */

public class ClassWorkFragment extends Fragment {
    View view;
    ListView homeWork;
    HomeWorkAdapter homeWorkAdapter;
    String classId;
    QMUIPullRefreshLayout pullRefreshLayout;
    private GetHomeWorkPresenter homeWorkPresenter=new GetHomeWorkPresenter(this);
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_classwork,container,false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initPullrefresh();
        classId=getArguments().getString("classid");
        homeWorkPresenter.getHomeWork(classId);
    }

    private void initPullrefresh(){
        pullRefreshLayout=view.findViewById(R.id.pull_refresh);
        pullRefreshLayout.setRefreshOffsetCalculator(new QMUICenterGravityRefreshOffsetCalculator());
        pullRefreshLayout.setOnPullListener(new QMUIPullRefreshLayout.OnPullListener() {
            @Override
            public void onMoveTarget(int offset) {

            }

            @Override
            public void onMoveRefreshView(int offset) {

            }

            @Override
            public void onRefresh() {
                homeWorkPresenter.getHomeWork(classId);
            }
        });
    }

    public void initListView(final ArrayList<HomeWork> arrayList){
        homeWork=view.findViewById(R.id.lv_classwork);
        homeWorkAdapter=new HomeWorkAdapter(getContext(),R.layout.item_homework,arrayList);
        homeWork.setAdapter(homeWorkAdapter);
        pullRefreshLayout.finishRefresh();
        homeWork.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle=new Bundle();
                bundle.putInt("type",2);
                bundle.putInt("testid",arrayList.get(i).getId());
                Intent intent=new Intent(getActivity(),PracticeActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}
